package com.sun.blog.sunblog.controller;

import java.io.IOException;

import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import freemarker.template.TemplateHashModel;
/**
 * 扩展FreeMarkerConfigurer，添加自定义枚举和静态方法
 * @author Songful
 * 在ftl文件中可以直接使用${MyConstants.USER_ADMIN!}, ${MyConstantsStatic.getValueByName("USER_ADMIN")}
 * 获取枚举USER_ADMIN对应的名称和对应的值
 */
public class ExtendsFreemarkerConfigurer extends FreeMarkerConfigurer{

	private Configuration configuration;
	@Override
	public void afterPropertiesSet() throws IOException, TemplateException {
		super.afterPropertiesSet();
		configuration = super.getConfiguration();

		BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
		TemplateHashModel enumModels = wrapper.getEnumModels();
		TemplateHashModel roundingModeEnums = (TemplateHashModel) enumModels
				.get("com.sun.blog.sunblog.base.MyConstants");

		configuration.setSharedVariable("MyConstants", roundingModeEnums);

		TemplateHashModel staticModels = wrapper.getStaticModels();

		TemplateHashModel ConstantsStatics = (TemplateHashModel) staticModels
				.get("com.sun.blog.sunblog.base.MyConstants");
		configuration.setSharedVariable("MyConstantsStatic", ConstantsStatics);

	}

}
